@bare-ts/lib 0.4.1 → 0.6.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.
- package/README.md +9 -10
- package/dist/codec/data.d.ts +1 -1
- package/dist/codec/data.js +11 -15
- package/dist/codec/f32-array.d.ts +5 -0
- package/dist/codec/f32-array.js +51 -0
- package/dist/codec/f64-array.d.ts +5 -0
- package/dist/codec/f64-array.js +51 -0
- package/dist/codec/{primitive.d.ts → fixed-primitive.d.ts} +1 -11
- package/dist/codec/fixed-primitive.js +197 -0
- package/dist/codec/i16-array.d.ts +4 -7
- package/dist/codec/i16-array.js +32 -33
- package/dist/codec/i32-array.d.ts +4 -7
- package/dist/codec/i32-array.js +32 -33
- package/dist/codec/i64-array.d.ts +4 -7
- package/dist/codec/i64-array.js +32 -33
- package/dist/codec/i8-array.d.ts +3 -3
- package/dist/codec/i8-array.js +12 -11
- package/dist/codec/int.d.ts +5 -0
- package/dist/codec/int.js +78 -0
- package/dist/codec/string.d.ts +1 -1
- package/dist/codec/string.js +144 -115
- package/dist/codec/u16-array.d.ts +4 -7
- package/dist/codec/u16-array.js +32 -33
- package/dist/codec/u32-array.d.ts +4 -7
- package/dist/codec/u32-array.js +32 -33
- package/dist/codec/u64-array.d.ts +4 -7
- package/dist/codec/u64-array.js +32 -33
- package/dist/codec/u8-array.d.ts +3 -3
- package/dist/codec/u8-array.js +26 -20
- package/dist/codec/u8-clamped-array.d.ts +3 -3
- package/dist/codec/u8-clamped-array.js +12 -11
- package/dist/codec/uint.d.ts +8 -0
- package/dist/codec/uint.js +138 -0
- package/dist/core/bare-error.js +12 -8
- package/dist/core/byte-cursor.d.ts +1 -1
- package/dist/core/byte-cursor.js +103 -31
- package/dist/core/config.d.ts +2 -4
- package/dist/core/config.js +14 -18
- package/dist/index.cjs +194 -134
- package/dist/index.d.cts +21 -16
- package/dist/index.d.ts +21 -16
- package/dist/index.js +12 -6
- package/dist/util/assert.d.ts +10 -1
- package/dist/util/assert.js +24 -13
- package/dist/util/constants.js +6 -2
- package/dist/util/validator.d.ts +27 -0
- package/dist/util/validator.js +38 -10
- package/imports/dev.d.ts +4 -0
- package/imports/dev.development.d.ts +4 -0
- package/imports/dev.development.js +4 -0
- package/imports/dev.js +4 -0
- package/imports/dev.node.d.ts +4 -0
- package/imports/dev.node.js +4 -0
- package/package.json +22 -21
- package/dist/codec/float-array.d.ts +0 -14
- package/dist/codec/float-array.js +0 -93
- package/dist/codec/primitive.js +0 -423
- package/dist/env/dev.d.ts +0 -1
- package/dist/env/dev.development.d.ts +0 -1
- package/dist/env/dev.development.js +0 -2
- package/dist/env/dev.js +0 -2
- package/dist/env/dev.node.d.ts +0 -1
- package/dist/env/dev.node.js +0 -2
package/dist/core/config.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
3
|
+
import { assert, DEV } from "../util/assert.js";
|
|
3
4
|
import { TOO_LARGE_NUMBER } from "../util/constants.js";
|
|
4
5
|
import { isU32 } from "../util/validator.js";
|
|
5
|
-
export function Config({
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
initialBufferLength,
|
|
19
|
-
maxBufferLength
|
|
20
|
-
};
|
|
6
|
+
export function Config({ initialBufferLength = 1024, maxBufferLength = 1024 * 1024 * 32 /* 32 MiB */, }) {
|
|
7
|
+
if (DEV) {
|
|
8
|
+
assert(isU32(initialBufferLength), TOO_LARGE_NUMBER);
|
|
9
|
+
assert(isU32(maxBufferLength), TOO_LARGE_NUMBER);
|
|
10
|
+
assert(initialBufferLength <= maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
initialBufferLength,
|
|
14
|
+
maxBufferLength,
|
|
15
|
+
};
|
|
21
16
|
}
|
|
17
|
+
export const DEFAULT_CONFIG = /* @__PURE__ */ Config({});
|
package/dist/index.cjs
CHANGED
|
@@ -18,12 +18,25 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AssertionError: () => AssertionError,
|
|
23
24
|
BareError: () => BareError,
|
|
24
25
|
ByteCursor: () => ByteCursor,
|
|
25
26
|
Config: () => Config,
|
|
27
|
+
DEFAULT_CONFIG: () => DEFAULT_CONFIG,
|
|
28
|
+
DEV: () => DEV,
|
|
29
|
+
assert: () => assert,
|
|
26
30
|
check: () => check,
|
|
31
|
+
isI16: () => isI16,
|
|
32
|
+
isI32: () => isI32,
|
|
33
|
+
isI64: () => isI64,
|
|
34
|
+
isI8: () => isI8,
|
|
35
|
+
isU16: () => isU16,
|
|
36
|
+
isU32: () => isU32,
|
|
37
|
+
isU64: () => isU64,
|
|
38
|
+
isU64Safe: () => isU64Safe,
|
|
39
|
+
isU8: () => isU8,
|
|
27
40
|
readBool: () => readBool,
|
|
28
41
|
readData: () => readData,
|
|
29
42
|
readF32: () => readF32,
|
|
@@ -113,30 +126,29 @@ __export(src_exports, {
|
|
|
113
126
|
writeU8FixedArray: () => writeU8FixedArray,
|
|
114
127
|
writeUint: () => writeUint,
|
|
115
128
|
writeUintSafe: () => writeUintSafe,
|
|
116
|
-
writeUintSafe32: () => writeUintSafe32
|
|
129
|
+
writeUintSafe32: () => writeUintSafe32,
|
|
130
|
+
writeUintTruncated: () => writeUintTruncated
|
|
117
131
|
});
|
|
118
|
-
module.exports = __toCommonJS(
|
|
132
|
+
module.exports = __toCommonJS(index_exports);
|
|
119
133
|
|
|
120
|
-
//
|
|
134
|
+
// imports/dev.node.js
|
|
121
135
|
var DEV = process.env.NODE_ENV === "development";
|
|
122
136
|
|
|
123
137
|
// src/util/assert.ts
|
|
124
|
-
var AssertionError = class extends Error {
|
|
125
|
-
constructor() {
|
|
126
|
-
super(...arguments);
|
|
127
|
-
this.name = "AssertionError";
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
138
|
var V8Error = Error;
|
|
131
139
|
function assert(test, message = "") {
|
|
132
140
|
if (!test) {
|
|
133
141
|
const e = new AssertionError(message);
|
|
134
|
-
|
|
135
|
-
V8Error.captureStackTrace(e, assert);
|
|
136
|
-
}
|
|
142
|
+
V8Error.captureStackTrace?.(e, assert);
|
|
137
143
|
throw e;
|
|
138
144
|
}
|
|
139
145
|
}
|
|
146
|
+
var AssertionError = class extends Error {
|
|
147
|
+
constructor() {
|
|
148
|
+
super(...arguments);
|
|
149
|
+
this.name = "AssertionError";
|
|
150
|
+
}
|
|
151
|
+
};
|
|
140
152
|
|
|
141
153
|
// src/util/validator.ts
|
|
142
154
|
function isI8(val) {
|
|
@@ -222,25 +234,50 @@ function reserve(bc, min) {
|
|
|
222
234
|
}
|
|
223
235
|
const minLen = bc.offset + min | 0;
|
|
224
236
|
if (minLen > bc.bytes.length) {
|
|
225
|
-
|
|
226
|
-
|
|
237
|
+
grow(bc, minLen);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function grow(bc, minLen) {
|
|
241
|
+
if (minLen > bc.config.maxBufferLength) {
|
|
242
|
+
throw new BareError(0, TOO_LARGE_BUFFER);
|
|
243
|
+
}
|
|
244
|
+
const buffer = bc.bytes.buffer;
|
|
245
|
+
let newBytes;
|
|
246
|
+
if (isEs2024ArrayBufferLike(buffer) && // Make sure that the view covers the end of the buffer.
|
|
247
|
+
// If it is not the case, this indicates that the user don't want
|
|
248
|
+
// to override the trailing bytes.
|
|
249
|
+
bc.bytes.byteOffset + bc.bytes.byteLength === buffer.byteLength && bc.bytes.byteLength + minLen <= buffer.maxByteLength) {
|
|
250
|
+
const newLen = Math.min(
|
|
251
|
+
minLen << 1,
|
|
252
|
+
bc.config.maxBufferLength,
|
|
253
|
+
buffer.maxByteLength
|
|
254
|
+
);
|
|
255
|
+
if (buffer instanceof ArrayBuffer) {
|
|
256
|
+
buffer.resize(newLen);
|
|
257
|
+
} else {
|
|
258
|
+
buffer.grow(newLen);
|
|
227
259
|
}
|
|
260
|
+
newBytes = new Uint8Array(buffer, bc.bytes.byteOffset, newLen);
|
|
261
|
+
} else {
|
|
228
262
|
const newLen = Math.min(minLen << 1, bc.config.maxBufferLength);
|
|
229
|
-
|
|
263
|
+
newBytes = new Uint8Array(newLen);
|
|
230
264
|
newBytes.set(bc.bytes);
|
|
231
|
-
bc.bytes = newBytes;
|
|
232
|
-
bc.view = new DataView(newBytes.buffer);
|
|
233
265
|
}
|
|
266
|
+
bc.bytes = newBytes;
|
|
267
|
+
bc.view = new DataView(newBytes.buffer);
|
|
268
|
+
}
|
|
269
|
+
function isEs2024ArrayBufferLike(buffer) {
|
|
270
|
+
return "maxByteLength" in buffer;
|
|
234
271
|
}
|
|
235
272
|
|
|
236
|
-
// src/codec/primitive.ts
|
|
273
|
+
// src/codec/fixed-primitive.ts
|
|
237
274
|
function readBool(bc) {
|
|
238
275
|
const val = readU8(bc);
|
|
239
276
|
if (val > 1) {
|
|
240
277
|
bc.offset--;
|
|
241
278
|
throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
|
|
242
279
|
}
|
|
243
|
-
return val
|
|
280
|
+
return val > 0;
|
|
244
281
|
}
|
|
245
282
|
function writeBool(bc, x) {
|
|
246
283
|
writeU8(bc, x ? 1 : 0);
|
|
@@ -356,74 +393,6 @@ function writeI64Safe(bc, x) {
|
|
|
356
393
|
}
|
|
357
394
|
writeU32(bc, highest32);
|
|
358
395
|
}
|
|
359
|
-
function readInt(bc) {
|
|
360
|
-
const zigZag = readUint(bc);
|
|
361
|
-
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
362
|
-
}
|
|
363
|
-
function writeInt(bc, x) {
|
|
364
|
-
const truncated = BigInt.asIntN(64, x);
|
|
365
|
-
if (DEV) {
|
|
366
|
-
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
367
|
-
}
|
|
368
|
-
const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
|
|
369
|
-
writeTruncatedUint(bc, zigZag);
|
|
370
|
-
}
|
|
371
|
-
function readIntSafe(bc) {
|
|
372
|
-
const firstByte = readU8(bc);
|
|
373
|
-
let result = (firstByte & 127) >> 1;
|
|
374
|
-
if (firstByte >= 128) {
|
|
375
|
-
let shiftMul = (
|
|
376
|
-
/* 2**6 */
|
|
377
|
-
64
|
|
378
|
-
);
|
|
379
|
-
let byteCount = 1;
|
|
380
|
-
let byte;
|
|
381
|
-
do {
|
|
382
|
-
byte = readU8(bc);
|
|
383
|
-
result += (byte & 127) * shiftMul;
|
|
384
|
-
shiftMul *= /* 2**7 */
|
|
385
|
-
128;
|
|
386
|
-
byteCount++;
|
|
387
|
-
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
388
|
-
if (byte === 0) {
|
|
389
|
-
bc.offset -= byteCount - 1;
|
|
390
|
-
throw new BareError(bc.offset, "must be canonical");
|
|
391
|
-
}
|
|
392
|
-
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
|
|
393
|
-
bc.offset -= byteCount - 1;
|
|
394
|
-
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
const isNeg = (firstByte & 1) === 1;
|
|
398
|
-
if (isNeg) {
|
|
399
|
-
result = -result - 1;
|
|
400
|
-
}
|
|
401
|
-
return result;
|
|
402
|
-
}
|
|
403
|
-
function writeIntSafe(bc, x) {
|
|
404
|
-
const sign = x < 0 ? 1 : 0;
|
|
405
|
-
let zigZag = x < 0 ? -(x + 1) : x;
|
|
406
|
-
let first7Bits = (zigZag & 63) << 1 | sign;
|
|
407
|
-
zigZag = Math.floor(zigZag / /* 2**6 */
|
|
408
|
-
64);
|
|
409
|
-
if (zigZag > 0) {
|
|
410
|
-
if (!Number.isSafeInteger(x)) {
|
|
411
|
-
if (DEV) {
|
|
412
|
-
assert(false, TOO_LARGE_NUMBER);
|
|
413
|
-
}
|
|
414
|
-
const low = zigZag & 32767;
|
|
415
|
-
const high = (zigZag / 32768 >>> 0) * 32768;
|
|
416
|
-
if (first7Bits === 127 && low === 32767 && high === 4294967295) {
|
|
417
|
-
first7Bits &= ~2;
|
|
418
|
-
}
|
|
419
|
-
zigZag = high + low;
|
|
420
|
-
}
|
|
421
|
-
writeU8(bc, 128 | first7Bits);
|
|
422
|
-
writeUintSafe(bc, zigZag);
|
|
423
|
-
} else {
|
|
424
|
-
writeU8(bc, first7Bits);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
396
|
function readU8(bc) {
|
|
428
397
|
check(bc, 1);
|
|
429
398
|
return bc.bytes[bc.offset++];
|
|
@@ -495,6 +464,8 @@ function writeU64Safe(bc, x) {
|
|
|
495
464
|
4294967296 & /* 2**21-1 */
|
|
496
465
|
2097151);
|
|
497
466
|
}
|
|
467
|
+
|
|
468
|
+
// src/codec/uint.ts
|
|
498
469
|
function readUint(bc) {
|
|
499
470
|
let low = readU8(bc);
|
|
500
471
|
if (low >= 128) {
|
|
@@ -531,13 +502,13 @@ function writeUint(bc, x) {
|
|
|
531
502
|
if (DEV) {
|
|
532
503
|
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
533
504
|
}
|
|
534
|
-
|
|
505
|
+
writeUintTruncated(bc, truncated);
|
|
535
506
|
}
|
|
536
|
-
function
|
|
507
|
+
function writeUintTruncated(bc, x) {
|
|
537
508
|
let tmp = Number(BigInt.asUintN(7 * 7, x));
|
|
538
509
|
let rest = Number(x >> BigInt(7 * 7));
|
|
539
510
|
let byteCount = 0;
|
|
540
|
-
while (tmp >= 128 || rest
|
|
511
|
+
while (tmp >= 128 || rest > 0) {
|
|
541
512
|
writeU8(bc, 128 | tmp & 127);
|
|
542
513
|
tmp = Math.floor(tmp / /* 2**7 */
|
|
543
514
|
128);
|
|
@@ -582,7 +553,7 @@ function writeUintSafe32(bc, x) {
|
|
|
582
553
|
}
|
|
583
554
|
let zigZag = x >>> 0;
|
|
584
555
|
while (zigZag >= 128) {
|
|
585
|
-
writeU8(bc, 128 |
|
|
556
|
+
writeU8(bc, 128 | x & 127);
|
|
586
557
|
zigZag >>>= 7;
|
|
587
558
|
}
|
|
588
559
|
writeU8(bc, zigZag);
|
|
@@ -649,7 +620,7 @@ function readU8FixedArray(bc, len) {
|
|
|
649
620
|
}
|
|
650
621
|
function writeU8FixedArray(bc, x) {
|
|
651
622
|
const len = x.length;
|
|
652
|
-
if (len
|
|
623
|
+
if (len > 0) {
|
|
653
624
|
reserve(bc, len);
|
|
654
625
|
bc.bytes.set(x, bc.offset);
|
|
655
626
|
bc.offset += len;
|
|
@@ -682,7 +653,7 @@ function writeFixedData(bc, x) {
|
|
|
682
653
|
writeU8FixedArray(bc, new Uint8Array(x));
|
|
683
654
|
}
|
|
684
655
|
|
|
685
|
-
// src/codec/
|
|
656
|
+
// src/codec/f32-array.ts
|
|
686
657
|
var readF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF32FixedArrayLe : readF32FixedArrayBe;
|
|
687
658
|
function readF32FixedArrayLe(bc, len) {
|
|
688
659
|
if (DEV) {
|
|
@@ -718,10 +689,12 @@ function readF32Array(bc) {
|
|
|
718
689
|
}
|
|
719
690
|
function writeF32Array(bc, x) {
|
|
720
691
|
writeUintSafe32(bc, x.length);
|
|
721
|
-
if (x.length
|
|
692
|
+
if (x.length > 0) {
|
|
722
693
|
writeF32FixedArray(bc, x);
|
|
723
694
|
}
|
|
724
695
|
}
|
|
696
|
+
|
|
697
|
+
// src/codec/f64-array.ts
|
|
725
698
|
var readF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readF64FixedArrayLe : readF64FixedArrayBe;
|
|
726
699
|
function readF64FixedArrayLe(bc, len) {
|
|
727
700
|
if (DEV) {
|
|
@@ -757,11 +730,29 @@ function readF64Array(bc) {
|
|
|
757
730
|
}
|
|
758
731
|
function writeF64Array(bc, x) {
|
|
759
732
|
writeUintSafe32(bc, x.length);
|
|
760
|
-
if (x.length
|
|
733
|
+
if (x.length > 0) {
|
|
761
734
|
writeF64FixedArray(bc, x);
|
|
762
735
|
}
|
|
763
736
|
}
|
|
764
737
|
|
|
738
|
+
// src/codec/i8-array.ts
|
|
739
|
+
function readI8Array(bc) {
|
|
740
|
+
return readI8FixedArray(bc, readUintSafe(bc));
|
|
741
|
+
}
|
|
742
|
+
function writeI8Array(bc, x) {
|
|
743
|
+
writeUintSafe32(bc, x.length);
|
|
744
|
+
writeI8FixedArray(bc, x);
|
|
745
|
+
}
|
|
746
|
+
function readI8FixedArray(bc, len) {
|
|
747
|
+
if (DEV) {
|
|
748
|
+
assert(isU32(len));
|
|
749
|
+
}
|
|
750
|
+
return new Int8Array(readFixedData(bc, len));
|
|
751
|
+
}
|
|
752
|
+
function writeI8FixedArray(bc, x) {
|
|
753
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
754
|
+
}
|
|
755
|
+
|
|
765
756
|
// src/codec/i16-array.ts
|
|
766
757
|
var readI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readI16FixedArrayLe : readI16FixedArrayBe;
|
|
767
758
|
function readI16Array(bc) {
|
|
@@ -788,7 +779,7 @@ function readI16FixedArrayBe(bc, len) {
|
|
|
788
779
|
var writeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI16FixedArrayLe : writeI16FixedArrayBe;
|
|
789
780
|
function writeI16Array(bc, x) {
|
|
790
781
|
writeUintSafe32(bc, x.length);
|
|
791
|
-
if (x.length
|
|
782
|
+
if (x.length > 0) {
|
|
792
783
|
writeI16FixedArray(bc, x);
|
|
793
784
|
}
|
|
794
785
|
}
|
|
@@ -828,7 +819,7 @@ function readI32FixedArrayBe(bc, len) {
|
|
|
828
819
|
var writeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI32FixedArrayLe : writeI32FixedArrayBe;
|
|
829
820
|
function writeI32Array(bc, x) {
|
|
830
821
|
writeUintSafe32(bc, x.length);
|
|
831
|
-
if (x.length
|
|
822
|
+
if (x.length > 0) {
|
|
832
823
|
writeI32FixedArray(bc, x);
|
|
833
824
|
}
|
|
834
825
|
}
|
|
@@ -868,7 +859,7 @@ function readI64FixedArrayBe(bc, len) {
|
|
|
868
859
|
var writeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeI64FixedArrayLe : writeI64FixedArrayBe;
|
|
869
860
|
function writeI64Array(bc, x) {
|
|
870
861
|
writeUintSafe32(bc, x.length);
|
|
871
|
-
if (x.length
|
|
862
|
+
if (x.length > 0) {
|
|
872
863
|
writeI64FixedArray(bc, x);
|
|
873
864
|
}
|
|
874
865
|
}
|
|
@@ -882,22 +873,74 @@ function writeI64FixedArrayBe(bc, x) {
|
|
|
882
873
|
}
|
|
883
874
|
}
|
|
884
875
|
|
|
885
|
-
// src/codec/
|
|
886
|
-
function
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
function writeI8Array(bc, x) {
|
|
890
|
-
writeUintSafe32(bc, x.length);
|
|
891
|
-
writeI8FixedArray(bc, x);
|
|
876
|
+
// src/codec/int.ts
|
|
877
|
+
function readInt(bc) {
|
|
878
|
+
const zigZag = readUint(bc);
|
|
879
|
+
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
892
880
|
}
|
|
893
|
-
function
|
|
881
|
+
function writeInt(bc, x) {
|
|
882
|
+
const truncated = BigInt.asIntN(64, x);
|
|
894
883
|
if (DEV) {
|
|
895
|
-
assert(
|
|
884
|
+
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
896
885
|
}
|
|
897
|
-
|
|
886
|
+
const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
|
|
887
|
+
writeUintTruncated(bc, zigZag);
|
|
898
888
|
}
|
|
899
|
-
function
|
|
900
|
-
|
|
889
|
+
function readIntSafe(bc) {
|
|
890
|
+
const firstByte = readU8(bc);
|
|
891
|
+
let result = (firstByte & 127) >> 1;
|
|
892
|
+
if (firstByte >= 128) {
|
|
893
|
+
let shiftMul = (
|
|
894
|
+
/* 2**6 */
|
|
895
|
+
64
|
|
896
|
+
);
|
|
897
|
+
let byteCount = 1;
|
|
898
|
+
let byte;
|
|
899
|
+
do {
|
|
900
|
+
byte = readU8(bc);
|
|
901
|
+
result += (byte & 127) * shiftMul;
|
|
902
|
+
shiftMul *= /* 2**7 */
|
|
903
|
+
128;
|
|
904
|
+
byteCount++;
|
|
905
|
+
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
906
|
+
if (byte === 0) {
|
|
907
|
+
bc.offset -= byteCount - 1;
|
|
908
|
+
throw new BareError(bc.offset, "must be canonical");
|
|
909
|
+
}
|
|
910
|
+
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
|
|
911
|
+
bc.offset -= byteCount - 1;
|
|
912
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
const isNeg = (firstByte & 1) === 1;
|
|
916
|
+
if (isNeg) {
|
|
917
|
+
result = -result - 1;
|
|
918
|
+
}
|
|
919
|
+
return result;
|
|
920
|
+
}
|
|
921
|
+
function writeIntSafe(bc, x) {
|
|
922
|
+
const sign = x < 0 ? 1 : 0;
|
|
923
|
+
let zigZag = x < 0 ? -(x + 1) : x;
|
|
924
|
+
let first7Bits = (zigZag & 63) << 1 | sign;
|
|
925
|
+
zigZag = Math.floor(zigZag / /* 2**6 */
|
|
926
|
+
64);
|
|
927
|
+
if (zigZag > 0) {
|
|
928
|
+
if (!Number.isSafeInteger(x)) {
|
|
929
|
+
if (DEV) {
|
|
930
|
+
assert(false, TOO_LARGE_NUMBER);
|
|
931
|
+
}
|
|
932
|
+
const low = zigZag & 32767;
|
|
933
|
+
const high = (zigZag / 32768 >>> 0) * 32768;
|
|
934
|
+
if (first7Bits === 127 && low === 32767 && high === 4294967295) {
|
|
935
|
+
first7Bits &= ~2;
|
|
936
|
+
}
|
|
937
|
+
zigZag = high + low;
|
|
938
|
+
}
|
|
939
|
+
writeU8(bc, 128 | first7Bits);
|
|
940
|
+
writeUintSafe(bc, zigZag);
|
|
941
|
+
} else {
|
|
942
|
+
writeU8(bc, first7Bits);
|
|
943
|
+
}
|
|
901
944
|
}
|
|
902
945
|
|
|
903
946
|
// src/codec/string.ts
|
|
@@ -1030,6 +1073,24 @@ function utf8ByteLength(s) {
|
|
|
1030
1073
|
var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
1031
1074
|
var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
|
|
1032
1075
|
|
|
1076
|
+
// src/codec/u8-clamped-array.ts
|
|
1077
|
+
function readU8ClampedArray(bc) {
|
|
1078
|
+
return readU8ClampedFixedArray(bc, readUintSafe32(bc));
|
|
1079
|
+
}
|
|
1080
|
+
function writeU8ClampedArray(bc, x) {
|
|
1081
|
+
writeUintSafe32(bc, x.length);
|
|
1082
|
+
writeU8ClampedFixedArray(bc, x);
|
|
1083
|
+
}
|
|
1084
|
+
function readU8ClampedFixedArray(bc, len) {
|
|
1085
|
+
if (DEV) {
|
|
1086
|
+
assert(isU32(len));
|
|
1087
|
+
}
|
|
1088
|
+
return new Uint8ClampedArray(readFixedData(bc, len));
|
|
1089
|
+
}
|
|
1090
|
+
function writeU8ClampedFixedArray(bc, x) {
|
|
1091
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1033
1094
|
// src/codec/u16-array.ts
|
|
1034
1095
|
var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
|
|
1035
1096
|
function readU16Array(bc) {
|
|
@@ -1056,7 +1117,7 @@ function readU16FixedArrayBe(bc, len) {
|
|
|
1056
1117
|
var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
|
|
1057
1118
|
function writeU16Array(bc, x) {
|
|
1058
1119
|
writeUintSafe32(bc, x.length);
|
|
1059
|
-
if (x.length
|
|
1120
|
+
if (x.length > 0) {
|
|
1060
1121
|
writeU16FixedArray(bc, x);
|
|
1061
1122
|
}
|
|
1062
1123
|
}
|
|
@@ -1096,7 +1157,7 @@ function readU32FixedArrayBe(bc, len) {
|
|
|
1096
1157
|
var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
|
|
1097
1158
|
function writeU32Array(bc, x) {
|
|
1098
1159
|
writeUintSafe32(bc, x.length);
|
|
1099
|
-
if (x.length
|
|
1160
|
+
if (x.length > 0) {
|
|
1100
1161
|
writeU32FixedArray(bc, x);
|
|
1101
1162
|
}
|
|
1102
1163
|
}
|
|
@@ -1136,7 +1197,7 @@ function readU64FixedArrayBe(bc, len) {
|
|
|
1136
1197
|
var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLe : writeU64FixedArrayBe;
|
|
1137
1198
|
function writeU64Array(bc, x) {
|
|
1138
1199
|
writeUintSafe32(bc, x.length);
|
|
1139
|
-
if (x.length
|
|
1200
|
+
if (x.length > 0) {
|
|
1140
1201
|
writeU64FixedArray(bc, x);
|
|
1141
1202
|
}
|
|
1142
1203
|
}
|
|
@@ -1150,24 +1211,6 @@ function writeU64FixedArrayBe(bc, x) {
|
|
|
1150
1211
|
}
|
|
1151
1212
|
}
|
|
1152
1213
|
|
|
1153
|
-
// src/codec/u8-clamped-array.ts
|
|
1154
|
-
function readU8ClampedArray(bc) {
|
|
1155
|
-
return readU8ClampedFixedArray(bc, readUintSafe32(bc));
|
|
1156
|
-
}
|
|
1157
|
-
function writeU8ClampedArray(bc, x) {
|
|
1158
|
-
writeUintSafe32(bc, x.length);
|
|
1159
|
-
writeU8ClampedFixedArray(bc, x);
|
|
1160
|
-
}
|
|
1161
|
-
function readU8ClampedFixedArray(bc, len) {
|
|
1162
|
-
if (DEV) {
|
|
1163
|
-
assert(isU32(len));
|
|
1164
|
-
}
|
|
1165
|
-
return new Uint8ClampedArray(readFixedData(bc, len));
|
|
1166
|
-
}
|
|
1167
|
-
function writeU8ClampedFixedArray(bc, x) {
|
|
1168
|
-
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
1214
|
// src/core/config.ts
|
|
1172
1215
|
function Config({
|
|
1173
1216
|
initialBufferLength = 1024,
|
|
@@ -1186,12 +1229,26 @@ function Config({
|
|
|
1186
1229
|
maxBufferLength
|
|
1187
1230
|
};
|
|
1188
1231
|
}
|
|
1232
|
+
var DEFAULT_CONFIG = /* @__PURE__ */ Config({});
|
|
1189
1233
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1190
1234
|
0 && (module.exports = {
|
|
1235
|
+
AssertionError,
|
|
1191
1236
|
BareError,
|
|
1192
1237
|
ByteCursor,
|
|
1193
1238
|
Config,
|
|
1239
|
+
DEFAULT_CONFIG,
|
|
1240
|
+
DEV,
|
|
1241
|
+
assert,
|
|
1194
1242
|
check,
|
|
1243
|
+
isI16,
|
|
1244
|
+
isI32,
|
|
1245
|
+
isI64,
|
|
1246
|
+
isI8,
|
|
1247
|
+
isU16,
|
|
1248
|
+
isU32,
|
|
1249
|
+
isU64,
|
|
1250
|
+
isU64Safe,
|
|
1251
|
+
isU8,
|
|
1195
1252
|
readBool,
|
|
1196
1253
|
readData,
|
|
1197
1254
|
readF32,
|
|
@@ -1281,5 +1338,8 @@ function Config({
|
|
|
1281
1338
|
writeU8FixedArray,
|
|
1282
1339
|
writeUint,
|
|
1283
1340
|
writeUintSafe,
|
|
1284
|
-
writeUintSafe32
|
|
1341
|
+
writeUintSafe32,
|
|
1342
|
+
writeUintTruncated
|
|
1285
1343
|
});
|
|
1344
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
1345
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
export * from "./codec/data.
|
|
2
|
-
export * from "./codec/
|
|
3
|
-
export * from "./codec/
|
|
4
|
-
export * from "./codec/
|
|
5
|
-
export * from "./codec/
|
|
6
|
-
export * from "./codec/
|
|
7
|
-
export * from "./codec/
|
|
8
|
-
export * from "./codec/
|
|
9
|
-
export * from "./codec/
|
|
10
|
-
export * from "./codec/
|
|
11
|
-
export * from "./codec/
|
|
12
|
-
export * from "./codec/u8-array.
|
|
13
|
-
export * from "./codec/
|
|
14
|
-
export * from "./
|
|
15
|
-
export * from "./
|
|
16
|
-
export * from "./
|
|
1
|
+
export * from "./codec/data.ts";
|
|
2
|
+
export * from "./codec/f32-array.ts";
|
|
3
|
+
export * from "./codec/f64-array.ts";
|
|
4
|
+
export * from "./codec/fixed-primitive.ts";
|
|
5
|
+
export * from "./codec/i8-array.ts";
|
|
6
|
+
export * from "./codec/i16-array.ts";
|
|
7
|
+
export * from "./codec/i32-array.ts";
|
|
8
|
+
export * from "./codec/i64-array.ts";
|
|
9
|
+
export * from "./codec/int.ts";
|
|
10
|
+
export * from "./codec/string.ts";
|
|
11
|
+
export * from "./codec/u8-array.ts";
|
|
12
|
+
export * from "./codec/u8-clamped-array.ts";
|
|
13
|
+
export * from "./codec/u16-array.ts";
|
|
14
|
+
export * from "./codec/u32-array.ts";
|
|
15
|
+
export * from "./codec/u64-array.ts";
|
|
16
|
+
export * from "./codec/uint.ts";
|
|
17
|
+
export * from "./core/bare-error.ts";
|
|
18
|
+
export * from "./core/byte-cursor.ts";
|
|
19
|
+
export * from "./core/config.ts";
|
|
20
|
+
export * from "./util/assert.ts";
|
|
21
|
+
export * from "./util/validator.ts";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
export * from "./codec/data.
|
|
2
|
-
export * from "./codec/
|
|
3
|
-
export * from "./codec/
|
|
4
|
-
export * from "./codec/
|
|
5
|
-
export * from "./codec/
|
|
6
|
-
export * from "./codec/
|
|
7
|
-
export * from "./codec/
|
|
8
|
-
export * from "./codec/
|
|
9
|
-
export * from "./codec/
|
|
10
|
-
export * from "./codec/
|
|
11
|
-
export * from "./codec/
|
|
12
|
-
export * from "./codec/u8-array.
|
|
13
|
-
export * from "./codec/
|
|
14
|
-
export * from "./
|
|
15
|
-
export * from "./
|
|
16
|
-
export * from "./
|
|
1
|
+
export * from "./codec/data.ts";
|
|
2
|
+
export * from "./codec/f32-array.ts";
|
|
3
|
+
export * from "./codec/f64-array.ts";
|
|
4
|
+
export * from "./codec/fixed-primitive.ts";
|
|
5
|
+
export * from "./codec/i8-array.ts";
|
|
6
|
+
export * from "./codec/i16-array.ts";
|
|
7
|
+
export * from "./codec/i32-array.ts";
|
|
8
|
+
export * from "./codec/i64-array.ts";
|
|
9
|
+
export * from "./codec/int.ts";
|
|
10
|
+
export * from "./codec/string.ts";
|
|
11
|
+
export * from "./codec/u8-array.ts";
|
|
12
|
+
export * from "./codec/u8-clamped-array.ts";
|
|
13
|
+
export * from "./codec/u16-array.ts";
|
|
14
|
+
export * from "./codec/u32-array.ts";
|
|
15
|
+
export * from "./codec/u64-array.ts";
|
|
16
|
+
export * from "./codec/uint.ts";
|
|
17
|
+
export * from "./core/bare-error.ts";
|
|
18
|
+
export * from "./core/byte-cursor.ts";
|
|
19
|
+
export * from "./core/config.ts";
|
|
20
|
+
export * from "./util/assert.ts";
|
|
21
|
+
export * from "./util/validator.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
//! Copyright (c) 2022 Victorien Elvinger
|
|
2
|
+
//! Licensed under the MIT License (https://mit-license.org/)
|
|
2
3
|
export * from "./codec/data.js";
|
|
3
|
-
export * from "./codec/
|
|
4
|
+
export * from "./codec/f32-array.js";
|
|
5
|
+
export * from "./codec/f64-array.js";
|
|
6
|
+
export * from "./codec/fixed-primitive.js";
|
|
7
|
+
export * from "./codec/i8-array.js";
|
|
4
8
|
export * from "./codec/i16-array.js";
|
|
5
9
|
export * from "./codec/i32-array.js";
|
|
6
10
|
export * from "./codec/i64-array.js";
|
|
7
|
-
export * from "./codec/
|
|
8
|
-
export * from "./codec/primitive.js";
|
|
11
|
+
export * from "./codec/int.js";
|
|
9
12
|
export * from "./codec/string.js";
|
|
13
|
+
export * from "./codec/u8-array.js";
|
|
14
|
+
export * from "./codec/u8-clamped-array.js";
|
|
10
15
|
export * from "./codec/u16-array.js";
|
|
11
16
|
export * from "./codec/u32-array.js";
|
|
12
17
|
export * from "./codec/u64-array.js";
|
|
13
|
-
export * from "./codec/
|
|
14
|
-
export * from "./codec/u8-clamped-array.js";
|
|
18
|
+
export * from "./codec/uint.js";
|
|
15
19
|
export * from "./core/bare-error.js";
|
|
16
20
|
export * from "./core/byte-cursor.js";
|
|
17
21
|
export * from "./core/config.js";
|
|
22
|
+
export * from "./util/assert.js";
|
|
23
|
+
export * from "./util/validator.js";
|
package/dist/util/assert.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
export { DEV } from "#dev";
|
|
2
2
|
/**
|
|
3
|
+
* @throws {AssertionError} if `test` is `false`.
|
|
4
|
+
* The message of the error is set to `message`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function assert(test: boolean, message?: string): asserts test;
|
|
7
|
+
/**
|
|
8
|
+
* Indicates the failure of an assertion.
|
|
9
|
+
* This error is thrown by {@link assert }.
|
|
10
|
+
*
|
|
11
|
+
* This error should not be caught.
|
|
12
|
+
*
|
|
3
13
|
* @sealed
|
|
4
14
|
*/
|
|
5
15
|
export declare class AssertionError extends Error {
|
|
6
16
|
name: string;
|
|
7
17
|
}
|
|
8
|
-
export declare function assert(test: boolean, message?: string): asserts test;
|